<template>
  <el-dialog
    :title="!dataForm.no ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="厂家名称" prop="cpyname">
      <el-input v-model="dataForm.cpyname" placeholder="厂家名称"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="cncname">
      <el-input v-model="dataForm.cncname" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备类型" prop="cnctype">
      <el-input v-model="dataForm.cnctype" placeholder="设备类型"></el-input>
    </el-form-item>
    <el-form-item label="工作状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="工作状态"></el-input>
    </el-form-item>
    <el-form-item label="焊接电流" prop="welcur">
      <el-input v-model="dataForm.welcur" placeholder="焊接电流"></el-input>
    </el-form-item>
    <el-form-item label="焊接电压" prop="welvol">
      <el-input v-model="dataForm.welvol" placeholder="焊接电压"></el-input>
    </el-form-item>
    <el-form-item label="内部温度" prop="temperature">
      <el-input v-model="dataForm.temperature" placeholder="内部温度"></el-input>
    </el-form-item>
    <el-form-item label="内部湿度" prop="tumidity">
      <el-input v-model="dataForm.tumidity" placeholder="内部湿度"></el-input>
    </el-form-item>
    <el-form-item label="气流量" prop="rempos">
      <el-input v-model="dataForm.rempos" placeholder="气流量"></el-input>
    </el-form-item>
    <el-form-item label="送丝速度" prop="curpgm">
      <el-input v-model="dataForm.curpgm" placeholder="送丝速度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          no: 0,
          cpyname: '',
          cncname: '',
          cnctype: '',
          status: '',
          welcur: '',
          welvol: '',
          temperature: '',
          tumidity: '',
          rempos: '',
          curpgm: ''
        },
        dataRule: {
          cpyname: [
            { required: true, message: '厂家名称不能为空', trigger: 'blur' }
          ],
          cncname: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          cnctype: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '工作状态不能为空', trigger: 'blur' }
          ],
          welcur: [
            { required: true, message: '焊接电流不能为空', trigger: 'blur' }
          ],
          welvol: [
            { required: true, message: '焊接电压不能为空', trigger: 'blur' }
          ],
          temperature: [
            { required: true, message: '内部温度不能为空', trigger: 'blur' }
          ],
          tumidity: [
            { required: true, message: '内部湿度不能为空', trigger: 'blur' }
          ],
          rempos: [
            { required: true, message: '气流量不能为空', trigger: 'blur' }
          ],
          curpgm: [
            { required: true, message: '送丝速度不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.no = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.no) {
            this.$http({
              url: this.$http.adornUrl(`/weldingmachinehisdata/weldingmachinehisdata/info/${this.dataForm.no}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cpyname = data.weldingmachineHisdata.cpyname
                this.dataForm.cncname = data.weldingmachineHisdata.cncname
                this.dataForm.cnctype = data.weldingmachineHisdata.cnctype
                this.dataForm.status = data.weldingmachineHisdata.status
                this.dataForm.welcur = data.weldingmachineHisdata.welcur
                this.dataForm.welvol = data.weldingmachineHisdata.welvol
                this.dataForm.temperature = data.weldingmachineHisdata.temperature
                this.dataForm.tumidity = data.weldingmachineHisdata.tumidity
                this.dataForm.rempos = data.weldingmachineHisdata.rempos
                this.dataForm.curpgm = data.weldingmachineHisdata.curpgm
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/weldingmachinehisdata/weldingmachinehisdata/${!this.dataForm.no ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'no': this.dataForm.no || undefined,
                'cpyname': this.dataForm.cpyname,
                'cncname': this.dataForm.cncname,
                'cnctype': this.dataForm.cnctype,
                'status': this.dataForm.status,
                'welcur': this.dataForm.welcur,
                'welvol': this.dataForm.welvol,
                'temperature': this.dataForm.temperature,
                'tumidity': this.dataForm.tumidity,
                'rempos': this.dataForm.rempos,
                'curpgm': this.dataForm.curpgm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
